package com.jiang.utils;


import com.jiang.bean.Song;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;


public class SongUtils {

    public static Document songToDocument(Song song) {

        Document document = new Document();

        StringField idfield = new StringField("id", String.valueOf(song.getId()), Field.Store.YES);
        TextField namefield = new TextField("name", song.getName(), Field.Store.YES);
        TextField hobbyfield = new TextField("url", song.getUrl(), Field.Store.YES);
        TextField albumfield = null;
        //如果专辑为空则专辑位置字面值为""
        // TODO 不添加这句判断到获取的专辑为空时就会出错,所有可能为空的属性都必须经过判断
        if (song.getAlbum() == null) {
            albumfield = new TextField("album", "", Field.Store.YES);
        } else {
            albumfield = new TextField("album", song.getAlbum(), Field.Store.YES);
        }
        TextField authorfield = new TextField("author", song.getAuthor(), Field.Store.YES);

        document.add(idfield);
        document.add(namefield);
        document.add(hobbyfield);
        document.add(albumfield);
        document.add(authorfield);

        return document;
    }
}
